package javaCode.chapter13.set_;

import java.util.LinkedHashSet;
import java.util.Objects;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings("all")
public class LinkedHashSetExercise {
    public static void main(String[] args) {
        LinkedHashSet linked = new LinkedHashSet();
        linked.add(new Car("张三",5000));
        linked.add(new Car("李四",2000));
        linked.add(new Car("张三",5000));

        System.out.println("linked=" + linked);
    }
}

/*
Car类(属性:name, price)，如果 name和 price一样，
则认为是相同元素，就不能添加。
 */
class Car{
    private String name;
    private double price;

    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Car car = (Car) o;
        return Double.compare(car.price, price) == 0 &&
                Objects.equals(name, car.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, price);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}